function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

$(document).ready(function () {
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });


    var id = getQueryString('id');
    if (id) {
        $.post(baseUrl + 'admin/sysAdmin/get', {id: id}, function (result) {
            var r = result.value;
            $("input[name='id']").val(r.id);
            $("input[name='userName']").val(r.userName);
            $("input[name='account']").val(r.account);
            $("input[name='password']").val(r.password);
            $("input[name='repeatPassword']").val(r.password);
            $("input[name='email']").val(r.email);
            $("input[name='phone']").val(r.phone);
            $('input:radio[name="isSystem"][value=' + r.isSystem + ']').iCheck('check');
            $('input:radio[name="isLocked"][value=' + r.isLocked + ']').iCheck('check');
        })
    }


    $("#btnSave").click(function () {
        var str = $("#myForm").serializeArray();
        $.ajax({
            url: baseUrl + 'admin/sysAdmin/save',
            type: 'post',
            data: str,
            success: function (data) {
                if (data.code === 0) {
                    goBack();
                } else {
                    layer.msg(data.message);
                }
            },
            error: function () {
                layer.msg("请求出错")
            }
        });
    });
});

function goBack() {
    location.href = baseUrl + 'admin/system/admin-list.html';
}